package kr.hkit.androidproject_caloriecheck;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import kr.hkit.androidproject_caloriecheck.R;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	public static final String DB_NANE = "User.db";
	public static final int DB_VERSION = 1;
	InputStream food;
	InputStream exercise;
	
	public DBHelper(Context context) {
		super(context, DB_NANE, null, DB_VERSION);
		food = context.getResources().openRawResource(R.raw.food);
		exercise = context.getResources().openRawResource(R.raw.exercise);
	}

	public void onCreate(SQLiteDatabase db) {		
		String sql = "create table food( "
				+ "_id integer primary key autoincrement, " 
				+ "food String, "
				+ "foodkcal integer" 
				+ ");";
		db.execSQL(sql);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(food));
		String str= null;
		try {
			while((str = br.readLine()) != null){
				db.execSQL(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		sql = "create table exercise( "
				+ "_id integer primary key autoincrement, " 
				+ "exercise String, "
				+ "exercisekcal integer" 
				+ ");";
		db.execSQL(sql);
		
		br = new BufferedReader(new InputStreamReader(exercise));
		try {
			while((str = br.readLine()) != null){
				db.execSQL(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "drop table if exists food";
		db.execSQL(sql);
		
		sql = "drop table if exists exercise";
		db.execSQL(sql);
		
		onCreate(db);
	}

}
